import {
  HTTP
} from '../utils/http.js';

export class ClassicModel extends HTTP {

  latestIndex = "latestIndex";

  getLatest(sCallBack) {
    this.request({
      url: "classic/latest",
      success: (res) => {
        sCallBack(res.data);
        this._setLatestIndex(res.data.index);
        wx.setStorageSync(this._getkey(res.data.index), res.data);
      }
    })
  }

  getClassic(index, nextOrPrevious, sCallback) {
    let key = nextOrPrevious == "next" ? this._getkey(index + 1) : this._getkey(index - 1);
    let classic = wx.getStorageSync(key);
    if (!classic) {
      this.request({
        url: `classic/${index}/${nextOrPrevious}`,
        success: (res) => {
          wx.setStorageSync(this._getkey(res.data.index), res.data);
          sCallback(res.data);
        }
      })
    } else {
      sCallback(classic);
    }
  }

  getMyFavor(success) {
    const params = {
      url: "classic/favor",
      success: success
    }
    this.request(params);
  }

  isFirst(index) {
    return index == 1 ? true : false;
  }

  isLatest(index) {
    const latestIndex = this._getLatestIndex();
    return index == latestIndex ? true : false;
  }

  _setLatestIndex(index) {
    wx.setStorageSync(this.latestIndex, index);
  }

  _getLatestIndex() {
    return wx.getStorageSync(this.latestIndex);
  }


  _getkey(index) {
    let key = "classic-" + index;
    return key;
  }


}